package cn.springboot.erp.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.springboot.erp.annotation.Log;
import cn.springboot.erp.commons.QueryParams;
import cn.springboot.erp.commons.ResponseResult;
import cn.springboot.erp.entity.Dept;
import cn.springboot.erp.enums.Operation;
import cn.springboot.erp.mapper.DeptMapper;
import cn.springboot.erp.service.DeptService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("dept")
@Api(tags = "部门管理")
public class DeptController {

    @Resource
    private DeptService deptService;

    @ApiOperation("获取部门列表")
    @GetMapping("deptName")
    @RequiresPermissions("sys:dept:select")
    public ResponseResult deptName(){
        List<Dept> list = deptService.list();
            return ResponseResult.Success().data("deptName",list);
    }

    @Log(description = Operation.SELECT,model = "部门管理")
    @ApiOperation("数据分页")
    @PostMapping("page")
    @RequiresPermissions("sys:dept:select")
    public ResponseResult listPage(@RequestBody QueryParams<Dept> queryParams){
        return deptService.listPage(queryParams);
    }

    @Log(description = Operation.SELECT,model = "部门管理")
    @ApiOperation("根据id查询")
    @GetMapping("selectById/{id}")
    @RequiresPermissions("sys:dept:select")
    public ResponseResult selectById(@PathVariable("id") Integer id){
        Dept dept = deptService.getById(id);
        return ResponseResult.Success().data("dept",dept);
    }

    @Log(description = Operation.ADDORUPDATE,model = "部门管理")
    @ApiOperation("添加或删除")
    @PostMapping("insertOrUpdate")
    @RequiresPermissions(value = {"sys:dept:add","sys:dept:update"})
    public ResponseResult insertOrUpdate(@RequestBody Dept dept){
        // 根据部门名称查询数据库
        String deptName = dept.getDeptName();
        LambdaQueryWrapper<Dept> lambda = new QueryWrapper<Dept>().lambda();
        lambda.eq(Dept::getDeptName,deptName);
        Dept deptOne = deptService.getOne(lambda);

        // 如果deptOne不为空，则表示部门名称已经存在
        if (ObjectUtils.isNotNull(deptOne)){
            // 如果id相同，做编辑操作
            if (ObjectUtil.equal(deptOne.getId(),dept.getId())){
                deptService.saveOrUpdate(dept);
                return ResponseResult.Success().msg("编辑成功");
            }
            // 否则返回部门名称存在的响应信息
            return ResponseResult.Fail().msg("部门名称已经存在");
        }
        // 如果deptOne为空，则表示部门名称不存在，做添加操作
        deptService.saveOrUpdate(dept);
        return ResponseResult.Success().msg("添加成功");
    }

    @Log(description = Operation.DELETE,model = "部门管理")
    @ApiOperation("根据id删除")
    @RequiresPermissions("sys:dept:delete")
    @GetMapping("deleteById/{id}")
    public ResponseResult deleteById(@PathVariable("id") Integer id){
        return deptService.deleteById(id);
    }
}
